package com.itstyle.blog.web;

import com.itstyle.blog.common.constant.SystemConstant;
import com.itstyle.blog.common.entity.Result;
import com.itstyle.blog.common.util.CommonUtils;
import com.itstyle.blog.entity.Blog;
import com.itstyle.blog.repository.BloglRepository;
import com.itstyle.blog.service.IBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 *  博文
 * 创建者 爪哇笔记
 */
@Controller
public class BlogController {

    @Autowired
    private BloglRepository bloglRepository;

    @Autowired
    private IBlogService blogService;

    /**
     * 博文
     */
    @RequestMapping("{id}.shtml")
    public String page(@PathVariable("id") Long id, ModelMap model) {
        Blog blog = blogService.getById(id);
        model.addAttribute("blog",blog);
        return  "article";
    }
    /**
     * 博文
     */
    @RequestMapping("{id}.html")
    public String blog(@PathVariable("id") Long id, ModelMap model) {
        Blog blog = blogService.getById(id);
        model.addAttribute("blog",blog);
        return  "article";
    }
    /**
     * 博文
     */
    @RequestMapping("list")
    @ResponseBody
    public Result list(@RequestBody Map<String, Object> params) {
        Integer pageNo = Integer.parseInt(params.get("pageNo").toString());
        pageNo = pageNo<1?1:pageNo;
        Pageable pageable = PageRequest.of(pageNo-1, SystemConstant.PAGE_SIZE);
        Page<Blog> list =  bloglRepository.findAll(pageable);
        return CommonUtils.msg(list);
    }
    /**
     * 博文
     */
    @RequestMapping("index")
    public String index() {
        return  "list";
    }

    /**
     * 博文
     */
    @RequestMapping("editor")
    public String editor() {
        return  "editor";
    }
}